package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取验证码
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R sendMsg( @RequestBody User user, HttpSession session ){
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            //随机生成验证码
            String code = RandomUtil.randomNumbers(6);
            //调用工具类发送短信
            //SMSUtils2.sendSms(phone,code);
//               session.setAttribute(phone,code);
//            //与数据库进行比较
            System.out.println(code);
            //需要将生成的验证码保存的redis中并设置保存时间
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("验证码发送成功");
        }
      return R.error("短信发送失败");
    }

    /**
     * 用户输入验证码,进行登录
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R login( @RequestBody Map<String,String> map,HttpSession session ){
        //获取手机号和用户输入的验证码
        String phone = map.get("phone");
        String code = map.get("code");
        if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(code)){
            return R.error("手机号或者验证码不能为空");
        }
        //从redis中获取验证码
        String codeSession = (String) redisTemplate.opsForValue().get(phone);
//        //获取session中保存的验证码,key是手机号
//        String codeSession = (String) session.getAttribute(phone);
        //校验两个验证码是否相同,如果相同,将用户信息存入到session中
        //手机号是没有注册的,自动保存到user表中
        if (code.equals(codeSession)){
            //先查询 select *from user where phone =?
            LambdaQueryWrapper<User> qw =new LambdaQueryWrapper<>();
            qw.eq(User::getPhone,phone);
            User one = userService.getOne(qw);
            if (one==null){
                one =new User();
                one.setPhone(phone);
                one.setStatus(1);
                userService.save(one);
            }
            session.setAttribute("loginPhone",one);
//            User user =new User();
//            user.setPhone(phone);
//            userService.save(user);
            //删除验证码
           redisTemplate.delete(phone);
            return R.success("登陆成功");
        }
        //比对失败：验证码错误，登录失败
        return R.error("验证码错误,登录失败");
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public R loginout(HttpSession session){
        session.removeAttribute("loginPhone");
        return R.success("退出成功");
    }
}
